<!-- AI模板 -->
<template>
  <div class="dashboard-editor-container">
    <div class="xxx" style="margin-top: 30px;">
      <el-tabs type="border-card" style="width:100%" @tab-click="handleTabClick">
        <el-tab-pane label="模板管理" style="background-color:#fff;">
          <div style="display:flex;justify-content:space-between;">
            <div
              style="background-color: #fff;width:225px; border: 1px solid #ccc;border-radius: 4px;box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);">
              <div style="display: flex; justify-content: space-between; align-items: center; padding: 10px;">
                <span style="color: #606266;font-size:15px">模板分类</span>
                <el-button plain @click="addModer" style="border: none;padding: 0;" size="mini" class="tooltip">
                  <span style="color: RGB(200, 200, 200);font-size: 25px;">+</span>
                </el-button>
              </div>
              <div style="margin-top:10px ;">
                <el-tree node-key="id" :data="nodeData" :defaultExpandAll="true" :highlight-current="true"
                  @node-click="handleNodeClick" :node-class="setNodeClass" :props="defaultProps" style="width:222px;">
                  <span slot-scope="{ node}" style="width: 90%;">
                    <span class="blue-on-hover" style="display: flex; align-items: center;">
                      <span v-show="node.level === 1" style="font-size:15px;font-family: Verdana;">{{ node.label }}</span>
                      <span v-show="node.level === 2" style="align-items: center;width: 180px;font-size:13px">
                        <span
                          style="width: 7px;height: 7px;border-radius: 50%;background-color: #e5e5e5;display: inline-block;margin-right: 5px;"></span>
                        {{ node.label }}
                        <span style="margin:0 4px">•</span>{{ node.data.Count }}
                      </span>
                      <template v-if="node.isCurrent">
                        <i class="el-icon-plus" @click.stop="addNew" style="margin-right:15px;font-size:13px"
                          slot="reference" v-show="node.level === 2" />
                      </template>
                      <template v-if="node.isCurrent">
                        <el-popconfirm confirm-button-text='确定' cancel-button-text='取消' title="确定此删除分类？"
                          @confirm="handleConfirm(node)">
                          <i class="el-icon-delete" style="margin-right:10px;font-size:13px" slot="reference"
                            v-show="node.level === 2" />
                        </el-popconfirm>
                      </template>
                    </span>
                  </span>
                  <span></span>
                </el-tree>
              </div>
            </div>
            <div style="width:calc(100% - 260px);">
              <el-tabs type="border-card" style="padding: 20px" @tab-click="handleClick" stretch>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-table stripe border :data="tableData" style="width: 100%; margin-top: 15px" height="620">

                    <el-table-column label="序号" width="60" type="index"
                      :index='(index) => { return (index + 1) + (pageNum - 1) * pageSize }'> </el-table-column>
                    <!-- <el-table-column label="隐藏或显示" width="100px" align="center">
                        <template slot-scope="scope">
                        <el-checkbox  @change="changeSelect(scope.row)" v-model="scope.row.isSelected" disabled>
                           <span :style="scope.row.isSelected ? { color: 'rgb(70, 166, 255)'}:{ color: 'gray'}">
                              {{ scope.row.isSelected ? '显示' : '隐藏'}}
                           </span>
                        </el-checkbox>
                        </template>
                      </el-table-column> -->
                    <el-table-column label="主题" prop="Title" width="180px" show-overflow-tooltip></el-table-column>
                    <el-table-column label="描述" prop="Description" width="200px" show-overflow-tooltip></el-table-column>
                    <el-table-column label="内容" prop="PromptContent" show-overflow-tooltip></el-table-column>
                    <el-table-column label="状态" width="80px" show-overflow-tooltip>
                      <template slot-scope="scope">
                        <span :style="scope.row.isSelected ? { color: 'rgb(70, 166, 255)' } : { color: 'gray' }">
                          {{ scope.row.isSelected ? '显示' : '隐藏' }}

                        </span>
                      </template>
                    </el-table-column>
                    <el-table-column label="操作" width="270px" fixed="right" type="opacity:"
                      style="position:fixed!important">
                      <template slot-scope="scope">
                        <div style="display: flex; justify-content: space-between;">
                          <el-button type="primary" size="mini" @click="changeCount(scope.row)">修改</el-button>
                          <el-button size="mini" @click="changeDisplay(scope.row)"
                            style="background-color:#e6a23c;color:#fff">显示</el-button>
                          <el-button type="info" size="mini" @click="changeHide(scope.row)">隐藏</el-button>
                          <el-button size="mini" type="danger" @click="deleteNode(scope.row)">删除</el-button>
                        </div>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-col>
              </el-tabs>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="角色管理" style="background-color:#fff;">
          <div style="display:flex;justify-content:space-between;">
            <div
              style="background-color: #fff;width:225px; border: 1px solid #ccc;border-radius: 4px;box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);">
              <div style="display: flex; justify-content: space-between; align-items: center; padding: 10px;">
                <span style="color: #606266;font-size:15px">模板分类</span>
                <el-button plain @click="addModer" style="border: none;padding: 0;" size="mini" class="tooltip">
                  <span style="color: RGB(200, 200, 200);font-size: 25px;">+</span>
                </el-button>
              </div>
              <div style="margin-top:10px ;">
                <el-tree node-key="id" :data="RoleData" :defaultExpandAll="true" :highlight-current="true"
                  @node-click="handleRoleNode" :node-class="setNodeClass" :props="defaultProps" style="width:222px;">
                  <span slot-scope="{ node }" style="width: 90%;">
                    <span class="blue-on-hover" style="display: flex; align-items: center;">
                      <span v-show="node.level === 1" style="font-size:15px;font-family: Verdana;">{{ node.label }}</span>
                      <span v-show="node.level === 2" style="align-items: center;width: 180px;font-size:13px">
                        <span
                          style="width: 7px;height: 7px;border-radius: 50%;background-color: #e5e5e5;display: inline-block;margin-right: 5px;"></span>
                        {{ node.label }}
                        <span style="margin:0 4px">•</span>{{ node.data.Count }}
                      </span>
                      <template v-if="node.isCurrent">
                        <i class="el-icon-plus" @click.stop="addNew" style="margin-right:15px;font-size:13px"
                          slot="reference" v-show="node.level === 2" />
                      </template>
                      <template v-if="node.isCurrent">
                        <el-popconfirm confirm-button-text='确定' cancel-button-text='取消' title="确定此删除分类？"
                          @confirm="handleConfirm(node)">
                          <i class="el-icon-delete" style="margin-right:10px;font-size:13px" slot="reference"
                            v-show="node.level === 2" />
                        </el-popconfirm>
                      </template>
                    </span>
                  </span>

                </el-tree>

              </div>

            </div>
            <div style="width:calc(100% - 260px);">
              <el-tabs type="border-card" style="padding: 20px" @tab-click="handleClick" stretch>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-table stripe border :data="tableRoleData" style="width: 100%; margin-top: 15px" height="620">
                    <el-table-column label="序号" width="60" type="index"
                      :index="(index) => { return (index + 1) + (pageNum - 1) * pageSize }">
                    </el-table-column>
                    <el-table-column label="主题" prop="Title" width="180px" show-overflow-tooltip></el-table-column>
                    <el-table-column label="描述" prop="SystemRole" show-overflow-tooltip></el-table-column>
                    <el-table-column label="状态" width="80px" show-overflow-tooltip>
                      <template slot-scope="scope">
                        <span :style="scope.row.isSelected ? { color: 'rgb(70, 166, 255)' } : { color: 'gray' }">
                          {{ scope.row.isSelected ? '显示' : '隐藏' }}

                        </span>
                      </template>
                    </el-table-column>
                    <el-table-column label="操作" width="270px" fixed="right" type="opacity:"
                      style="position:fixed!important">
                      <template slot-scope="scope">
                        <div style="display: flex; justify-content: space-between;">
                          <el-button type="primary" size="mini" @click="changeRole(scope.row)">修改</el-button>
                          <el-button size="mini" @click="changeDisplay(scope.row)"
                            style="background-color:#e6a23c;color:#fff">显示</el-button>
                          <el-button type="info" size="mini" @click="changeHide(scope.row)">隐藏</el-button>
                          <el-button size="mini" type="danger" @click="deleteNode(scope.row)">删除</el-button>
                        </div>

                      </template>

                    </el-table-column>
                  </el-table>
                </el-col>

              </el-tabs>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 创建模板分类 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="创建模板分类" :visible.sync="moderVisible"
      v-el-drag-dialog @close="Moder = {}" width="30%">
      <el-form label-height="430px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="Moder">
              <div style="color: red;">*</div>
              <el-input placeholder="请输入..." maxlength="10" v-model="Moder.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
            <el-col :span="24">
              <el-form-item label="描述" prop="Moder">
                <el-input
                  placeholder="请输入..."
                  type="textarea"
                  v-model="Moder.info"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addMo()" :disabled="!Moder.name">创建</el-button>
      </span>
    </el-dialog>
    <!-- 修改资源分类 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="修改资源" :visible.sync="categoryVisible"
      v-el-drag-dialog width="40%">
      <el-form label-width="120px" label-height="430px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="主题" prop="Category">
              <el-input :placeholder="Category.Title" v-model="totalCate.Title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="Category">
              <el-input :placeholder="Category.Description" v-model="totalCate.Description"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="Category">
              <el-input :placeholder="Category.PromptContent" v-model="totalCate.PromptContent" type="textarea"
                rows="4"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateCategory()">修改</el-button>
        <el-button type="info" @click="categoryVisible = false">取消</el-button>
      </span>
    </el-dialog>
    <!-- 修改角色分类 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="修改资源" :visible.sync="RoleVisible"
      v-el-drag-dialog width="40%">
      <el-form label-width="120px" label-height="430px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="主题" prop="Category">
              <el-input :placeholder="RoleCate.Title" v-model="RoleForm.Title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="Category">
              <el-input :placeholder="RoleCate.SystemRole" v-model="RoleForm.SystemRole" type="textarea"
                rows="4"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updaRole">修改</el-button>
        <el-button type="info" @click="RoleVisible = false">取消</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="添加资源" :visible.sync="reSourceVisible"
      v-el-drag-dialog width="40%">
      <el-form label-width="120px" label-height="430px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="主题" prop="Category">
              <el-input placeholder="请输入..." v-model="reSource.Title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="Category">
              <el-input placeholder="请输入..." v-model="reSource.Description"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="this.Action == 0">
          <el-col :span="24">
            <el-form-item label="内容" prop="Category">
              <el-input placeholder="请输入..." v-model="reSource.PromptContent" type="textarea" rows="4"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="是否隐藏" prop="Category">
              <el-checkbox v-model="reSource.isSelected"></el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateResource()">添加</el-button>
        <el-button type="info"
          @click="reSourceVisible = false, reSource = [], $set(reSource, 'isSelected', false)">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { connrequest } from '@/api/firstPage'
import elDragDialog from "@/directive/el-drag-dialog"; // base on element-ui
export default {
  directives: { elDragDialog },
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      tableRoleData: [],
      tablist: [],
      typeData: [],
      Dir: '',
      Filter: '',
      ischeck: false,
      isSelected: false,
      idMap: '',
      isBlock: [0],
      nodeData: [],
      RoleData: [],
      defaultProps: {
        children: 'children',
        label: 'Name',
        Count: 'Count'
      },
      selectedNode: null,
      moderVisible: false,
      Moder: {},
      Node: null,
      categoryVisible: false,
      Category: [],
      totalCate: [],
      RoleCate: [],
      RoleForm: [],
      RoleVisible: false,
      getNoname: '',
      Action: 0,
      reSourceVisible: false,
      reSource: [],
    }

  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.getData()
    this.getRoleData()
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
  },
  props: {
    defaultExpandAll: {
      type: Boolean,
      default: false,
      coerce: (value) => {
        if (typeof value === 'string') {
          return value.toLowerCase() === 'true';
        }
        return value;
      }
    }
  },

  methods: {
    async updateResource() {
      // this.reSource.IsNotShow = 1
      if (this.reSource.isSelected)
        this.reSource.IsNotShow = 1
      else
        this.reSource.IsNotShow = 0
      let row = 0
      if (this.Action == 0) {
        row = this.tableData.findIndex(
          (item) => item.Title === this.reSource.Title
        );
      }
      else {
        row = this.tableRoleData.findIndex(
          (item) => item.Title === this.reSource.Title
        );
      }
      if (row !== -1) {
        this.$message.error("主题不能重复");
        return;
      }
      if (this.reSource.Title === '' || this.reSource.Title === undefined || this.reSource.Title.trim() === '') {
        this.$message.error("主题不能为空");
        return;
      }
      if (this.reSource.Description === '' || this.reSource.Description === undefined || this.reSource.Description.trim() === '') {
        this.$message.error("描述不能为空");
        return
      }
      if (this.Action == 0)
        if (this.reSource.PromptContent === '' || this.reSource.PromptContent === undefined || this.reSource.PromptContent.trim() === '') {
          this.$message.error("内容不能为空");
          return;
        }
      if (this.Action == 0) {
        const data = {
          Data: {
            Action: 1,
            Detail: {
              Id: 0,
              Title: this.reSource.Title,
              Description: this.reSource.Description,
              PromptContent: this.reSource.PromptContent,
              ClassName: this.Node.Name,
              IsNotShow: this.reSource.IsNotShow
            }
          }
        }
        const res = await connrequest('EditAiTemplateRes', data)
        if (res.Status.StatusCode === 0) {
          this.$message.success('添加成功')
          this.GetShareFiles(this.Node.Name)
          this.Node.Count++
          this.reSourceVisible = false

        }
        this.reSource = []
      }
      else {
        const data = {
          Data: {
            Action: 1,
            Detail: {
              Id: 0,
              Title: this.reSource.Title,
              SystemRole: this.reSource.Description,
              ClassName: this.Node.Name,
              IsNotShow: this.reSource.IsNotShow
            }
          }
        }
        const res = await connrequest('EditAiRoleRes', data)
        if (res.Status.StatusCode === 0) {
          this.$message.success('添加成功')
          this.GetShareRoleFiles(this.Node.Name)
          this.Node.Count++
          this.reSourceVisible = false

        }
        this.reSource = []
      }
      this.reSource.isSelected = false
    },
    addNew() {
      this.reSourceVisible = true
    },
    async handleConfirm(node) {
      if (node.data.Count === 0) {
        if (this.Action == 0) {
          this.nodeData[0].children = this.nodeData[0].children.filter(item => item.Name !== node.label);
          this.$message.success('删除成功')
          this.Node = this.nodeData[0].children[0]
          this.GetShareFiles(this.Node.Name)
          return
        }
        else {
          this.RoleData[0].children = this.RoleData[0].children.filter(item => item.Name !== node.label);
          this.$message.success('删除成功')
          this.Node = this.RoleData[0].children[0]
          this.GetShareRoleFiles(this.Node.Name)
          return
        }

      }
      const data = {
        Data: {
          Action: 3,
          Detail: {
            Id: 0,
            ClassName: node.label,
          }
        }
      }
      let res = ''
      if (this.Action == 0)
        res = await connrequest('EditAiTemplateRes', data)
      else
        res = await connrequest('EditAiRoleRes', data)
      if (res.Status.StatusCode === 0) {
        this.$message.success('删除成功')
        if (this.Action == 0) this.getData()
        else this.getRoleData()
      }

    },
    changeinfo() {
    },
    async updaRole() {

      this.RoleForm.Id = this.RoleCate.Id
      this.RoleForm.IsNotShow = this.RoleCate.IsNotShow
      if (this.RoleForm.SystemRole === undefined) this.RoleForm.SystemRole = this.RoleCate.SystemRole
      if (this.RoleForm.Title === undefined) this.RoleForm.Title = this.RoleCate.Title

      if (this.Node === null)
        this.Node = this.RoleData[0].children[0]
      let row = 0
      if (this.RoleForm.Title !== this.RoleCate.Title) {
        row = this.tableRoleData.findIndex(
          (item) => item.Title === this.RoleForm.Title
        );
      }
      if (row !== -1) {
        this.$message.error("主题不能重复");
        this.RoleVisible = false
        return;
      }
      if (this.RoleForm.Title === '' || this.RoleForm.Title.trim() === '') {
        this.$message.error("主题不能为空");
        this.RoleVisible = false
        return;
      }
      if (this.RoleForm.SystemRole === '' || this.RoleForm.SystemRole.trim() === '') {
        this.$message.error("描述不能为空");
        this.RoleVisible = false
        return;
      }
      const data = {
        Data: {
          Action: 2,
          Detail: {
            Id: this.RoleForm.Id,
            Title: this.RoleForm.Title,
            SystemRole: this.RoleForm.SystemRole,
            ClassName: this.Node.Name,
            IsNotShow: this.RoleForm.IsNotShow
          }
        }
      }
      const res = await connrequest('EditAiRoleRes', data)
      if (res.Status.StatusCode === 0) {
        this.$message.success('修改成功')
        this.RoleVisible = false
        this.GetShareRoleFiles(this.Node.Name)
      }
    },
    handleTabClick(tab) {
      const loading = this.$loading({
        lock: true,
        text: '加载中',
        // spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      setTimeout(() => {
        loading.close();
      }, 1000);
      this.Action = tab.index
      this.Node = null


    },
    async changeDisplay(row) {
      if (this.Action == 0) {
        if (this.Node === null)
          this.Node = this.nodeData[0].children[0]
      }
      else {
        if (this.Node === null)
          this.Node = this.RoleData[0].children[0]
      }
      if (row.IsNotShow === 0)
        this.$message.info('当前资源处于显示的状态')
      else {
        if (this.Action == 0) {
          const data = {
            Data: {
              Action: 2,
              Detail: {
                Id: row.Id,
                Title: row.Title,
                Description: row.Description,
                PromptContent: row.PromptContent,
                ClassName: this.Node.Name,
                IsNotShow: 0
              }
            }
          }
          const res = await connrequest('EditAiTemplateRes', data)
          if (res.Status.StatusCode === 0) {
            this.$message.success('修改成功')
            this.GetShareFiles(this.Node.Name)
          }
        }
        else {
          const data = {
            Data: {
              Action: 2,
              Detail: {
                Id: row.Id,
                Title: row.Title,
                SystemRole: row.SystemRole,
                ClassName: this.Node.Name,
                IsNotShow: 0
              }
            }
          }
          const res = await connrequest('EditAiRoleRes', data)
          if (res.Status.StatusCode === 0) {
            this.$message.success('修改成功')
            this.GetShareRoleFiles(this.Node.Name)
          }
        }

      }

    },
    async changeHide(row) {
      if (this.Action == 0) {
        if (this.Node === null)
          this.Node = this.nodeData[0].children[0]
      }
      else {
        if (this.Node === null)
          this.Node = this.RoleData[0].children[0]
      }
      if (row.IsNotShow === 1)
        this.$message.info('当前资源处于隐藏的状态')
      else {
        if (this.Action == 0) {
          const data = {
            Data: {
              Action: 2,
              Detail: {
                Id: row.Id,
                Title: row.Title,
                Description: row.Description,
                PromptContent: row.PromptContent,
                ClassName: this.Node.Name,
                IsNotShow: 1
              }
            }
          }
          const res = await connrequest('EditAiTemplateRes', data)
          if (res.Status.StatusCode === 0) {
            this.$message.success('修改成功')
            this.GetShareFiles(this.Node.Name)
          }
        }
        else {
          const data = {
            Data: {
              Action: 2,
              Detail: {
                Id: row.Id,
                Title: row.Title,
                SystemRole: row.SystemRole,
                ClassName: this.Node.Name,
                IsNotShow: 1
              }
            }
          }
          const res = await connrequest('EditAiRoleRes', data)
          if (res.Status.StatusCode === 0) {
            this.$message.success('修改成功')
            this.GetShareRoleFiles(this.Node.Name)
          }
        }
      }
    },
    deleteNode(row) {
      this.$confirm('此操作将删除该资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (this.Action == 0) {
          if (this.Node === null)
            this.Node = this.nodeData[0].children[0]
        }
        else {
          if (this.Node === null)
            this.Node = this.RoleData[0].children[0]
        }
        if (this.Action == 0) {
          const data = {
            Data: {
              Action: 3,
              Detail: {
                Id: row.Id,
                Title: row.Title,
                Description: row.Description,
                PromptContent: row.PromptContent,
                ClassName: this.Node.Name,
                IsNotShow: row.IsNotShow
              }
            }
          }
          const res = await connrequest('EditAiTemplateRes', data)
          if (res.Status.StatusCode === 0) {
            this.Node.Count--
            if (this.Node.Count === 0)
              this.$message.info('删除成功，当前分类下没有资源，将不被保留')
            this.GetShareFiles(this.Node.Name)
          }
        }
        else {
          const data = {
            Data: {
              Action: 3,
              Detail: {
                Id: row.Id,
                Title: row.Title,
                SystemRole: row.SystemRole,
                ClassName: this.Node.Name,
                IsNotShow: row.IsNotShow
              }
            }
          }
          const res = await connrequest('EditAiRoleRes', data)
          if (res.Status.StatusCode === 0) {
            this.Node.Count--
            if (this.Node.Count === 0)
              this.$message.info('删除成功，当前分类下没有资源，将不被保留')
            this.GetShareRoleFiles(this.Node.Name)
          }
        }

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    async updateCategory() {
      this.totalCate.Id = this.Category.Id
      this.totalCate.IsNotShow = this.Category.IsNotShow
      if (this.totalCate.Description === undefined) this.totalCate.Description = this.Category.Description
      if (this.totalCate.PromptContent === undefined) this.totalCate.PromptContent = this.Category.PromptContent
      if (this.totalCate.Title === undefined) this.totalCate.Title = this.Category.Title

      if (this.Node === null)
        this.Node = this.nodeData[0].children[0]
      let row = -1
      if (this.totalCate.Title !== this.Category.Title) {
        row = this.tableData.findIndex(
          (item) => item.Title === this.totalCate.Title
        );
      }
      if (row !== -1) {
        this.$message.error("主题不能重复");
        this.categoryVisible = false
        return;
      }
      if (this.totalCate.Title === '' || this.totalCate.Title.trim() === '') {
        this.$message.error("主题不能为空");
        this.categoryVisible = false
        return;
      }
      if (this.totalCate.Description === '' || this.totalCate.Description.trim() === '') {
        this.$message.error("描述不能为空");
        this.categoryVisible = false
        return;
      }
      if (this.totalCate.PromptContent === '' || this.totalCate.PromptContent.trim() === '') {
        this.$message.error("内容不能为空");
        this.categoryVisible = false
        return;
      }
      const data = {
        Data: {
          Action: 2,
          Detail: {
            Id: this.totalCate.Id,
            Title: this.totalCate.Title,
            Description: this.totalCate.Description,
            PromptContent: this.totalCate.PromptContent,
            ClassName: this.Node.Name,
            IsNotShow: this.totalCate.IsNotShow
          }
        }
      }
      const res = await connrequest('EditAiTemplateRes', data)
      if (res.Status.StatusCode === 0) {
        this.$message.success('修改成功')
        this.categoryVisible = false
        this.GetShareFiles(this.Node.Name)
      }
    },
    async changeCount(row) {
      this.categoryVisible = true
      this.totalCate = []
      this.Category = row
    },
    changeRole(row) {
      this.RoleVisible = true
      this.RoleForm = []
      this.RoleCate = row
    },
    delModer() {
      this.$confirm('确定删除这个模板嘛', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    async addMo() {
      let data = {}
      data.id = this.nodeData[0].children.length
      data.Name = this.Moder.name
      data.Count = 0
      data.Name = data.Name.replace(/\s/g, "");
      let row = 0
      if (this.Action == 0) {
        row = this.nodeData[0].children.findIndex(
          (item) => item.Name === data.Name
        );
      }
      else {
        row = this.RoleData[0].children.findIndex(
          (item) => item.Name === data.Name
        );
      }
      if (row !== -1) {
        this.$message.error("分类名称不能重复");
        return;
      }
      if (data.Name === '') {
        this.$message.error("分类名称不能为空");
        return;
      }
      if (this.Action == 0)
        this.nodeData[0].children.push(data)
      else
        this.RoleData[0].children.push(data)
      this.moderVisible = false
      this.Moder.name = ''
    },
    addModer() {
      this.moderVisible = true
    },
    handleNodeClick(node) {
      if (node.Count === 0)
        this.$message.info('当前分类下没有资源，将不被保留')
      if (node.Id == 0)
        return
      this.selectedNode = node
      this.getNoname = node.Name
      this.GetShareFiles(node.Name)
      this.Node = node
    },
    handleRoleNode(node) {
      if (node.Count == 0)
        this.$message.info('当前分类下没有资源，将不被保留')
      if (node.Id == 0)
        return
      this.Node = node
      this.GetShareRoleFiles(node.Name)
    },
    setNodeClass(node) {
      // 根据选中状态设置节点的样式类
      return {
        'tree-node-selected': node === this.selectedNode
      }
    },
    async serveInfo() {
      if (false) {
        const data = {
          Data: [
            {
              Id: this.idMap.get(this.Node.Name),
              IsNotShow: 1
            }
          ]

        };
        try {
          const res = await connrequest('SetAiTemplateClassShowStatus', data)
          // this.getData()
          this.ischeck = true
          this.isBlock[this.idMap.get(this.Node.Name)] = 1
          this.GetShareFiles(this.Node.Name)
        }
        catch (error) {
          //console.error('json解析错误',error)
        }

      }
      else {

        let i = 0
        // 存储显示状态
        let list = {}
        // 存储对于Id以及显示状态
        let ResModifi = []
        let a = []
        this.tableData.map(item => {
          i += 1
          list.Id = i
          list.IsNotShow = item.IsNotShow
          if (item.isSelected) {
            list.IsNotShow = 0
          }
          else {
            list.IsNotShow = 1
          }
          let a = {}
          a.Id = list.Id
          a.IsNotShow = list.IsNotShow
          ResModifi.push(a)
        })
        if (this.Node === null)
          this.Node = this.nodeData[0].children[0]
        const data = {
          Data: {
            ClassName: this.Node.Name,
            ResModifi: ResModifi
          }
        }
        // const res = await connrequest('SetAiTemplateResShowStatus', data)
        // 大模板是否显示
        // const datas = {
        //   Data:[
        //       {
        //        Id: this.idMap.get(this.Node.Name),
        //        IsNotShow:0
        //        }
        //     ]

        // };
        // const res1 = await connrequest('SetAiTemplateClassShowStatus', datas)
        // this.ischeck = false
        // this.getData()
        this.GetShareFiles(this.Node.Name)
      }
    },
    changeCheck() {
      this.ischeck = !this.ischeck

    },
    // 改变当前行的选中状态
    changeSelect(row) {

    },
    async getData() {
      const res = await connrequest('GetAiTemplateClass')
      let tablist = []
      this.idMap = new Map()
      res.Data = res.Data || []
      this.isBlock = [0]
      let children = []
      res.Data.map(item => {
        tablist.push(item.Name)
        let data = {}
        data.id = item.Id
        data.Name = item.Name
        data.Count = item.Count
        children.push(data)
        this.idMap.set(item.Name, item.Id)
        if (item.IsNotShow !== undefined) {
          this.isBlock.push(item.IsNotShow)
        }
        else {
          this.isBlock.push(0)
        }
      })
      const datas = {
        Data: [
          {
            Id: 0,
            Name: '所有模板',
            children: children
          }
        ]

      };
      this.nodeData = []
      this.nodeData = datas.Data
      this.Dir = tablist[0]
      this.getNoname = this.Dir
      this.GetShareFiles(this.Dir)
      this.tablist = new Set(tablist)


    },
    async getRoleData() {
      // GetAiRoleClass
      const res = await connrequest('GetAiRoleClass')
      res.Data = res.Data || []
      let tablist = []
      let children = []
      res.Data.map(item => {
        tablist.push(item.Name)
        let data = {}
        data.id = item.Id
        data.Name = item.Name
        data.Count = item.Count
        children.push(data)
        // this.idMap.set(item.Name,item.Id)
      })
      const datas = {
        Data: [
          {
            Id: 0,
            Name: '角色模板',
            children: children
          }
        ]

      };
      this.Rolenode = []
      this.RoleData = datas.Data
      // this.getNoname = res.Data[0].Names
      this.GetShareRoleFiles(res.Data[0].Name)
    },
    async getList(name) {
      const data = {
        Data: {
          ResName: name
        }
      }
      const res = await connrequest('GetAiTemplateRes', data)
    },
    async GetShareFiles(name) {
      const data = {
        Data: {
          ResName: name
        }
      }
      const res = await connrequest('GetAiTemplateRes', data)
      res.Data = res.Data || []
      this.tableData = res.Data
      this.tableData.map(item => {
        if (item.IsNotShow) {
          this.$set(item, 'isSelected', false)
        }
        else
          this.$set(item, 'isSelected', true)

      })
    },
    async GetShareRoleFiles(name) {
      const data = {
        Data: {
          ResName: name
        }
      }
      const res = await connrequest('GetAiRoleRes', data)
      res.Data = res.Data || []
      this.tableRoleData = res.Data
      this.tableRoleData.map(item => {
        if (item.IsNotShow) {
          this.$set(item, 'isSelected', false)
        }
        else
          this.$set(item, 'isSelected', true)

      })
    },
    handleClick(tab) {
      this.Dir = tab.label
      this.ischeck = false
      this.GetShareFiles(tab.label)
    }
  }
};
</script>
<style scoped>
/* @import url(); 引入css类 */
.el-tree-node__content {
  height: 35px;
}

/* .tree-node-selected {
  background-color: yellow;
} */
.buton {
  border: none;
  padding: 0;
  /* background-color: blue; */
  width: 20px;
  margin-right: 5px;
  /* float: right; */
}

.buton2 {
  border: none;
  padding: 0;
  /* background-color: blue; */
  width: 20px;
  margin-right: 5px;
  float: right;
}

.blue-on-hover:hover {
  color: #62bfff;
  /* cursor: pointer; */
}

.tooltip {
  position: relative;
  display: inline-block;
  margin-bottom: 8px;
  /* 添加下方间距 */
}

.tooltip::after {
  content: '创建模板分类';
  position: absolute;
  top: calc(100% + 8px);
  /* 上方间距 */
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 4px;
  background-color: #333;
  color: #fff;
  font-size: 12px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
  z-index: 999;
}

.tooltip:hover::after {
  opacity: 1;
  visibility: visible;
}
</style>
